 /********************************************************************************************\
 * libeds - extensible data structures library                                                *
 * Copyright (C) 2006, Dan Kruchinin, <just.asgard@gmail.com>                                 *
 *                                                                                            *
 * -> test/tree/rnd_tree_test.c                                                               *
 *                                                                                            *
 * This library is free software; you can redistribute it and/or modify it under the terms of *
 * the GNU Lesser General Public License as published by the Free Software Foundation;        *
 * either version 2.1 of the License, or (at your option) any later version.                  *
 *                                                                                            *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;  *
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  *
 * See the GNU Lesser General Public License for more details.                                *
 *                                                                                            *
 * You should have received a copy of the GNU Lesser General Public License along with this   *
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,  *
 * Boston, MA 02111-1307 USA                                                                  *
 \********************************************************************************************/

#include <stdlib.h>
#include "ds.h"
//#include "safe_alloc.h"
//#include "errors.h"
#include "trees/rnd_tree.h"
#include "test/test_base.h"
#include "test/test_bin_tree.h"

#define MAX 16

ds_cmp_t _cmp(const void* key1, const void* key2)
{
  char k1, k2;

  k1 = *((char*)key1);
  k2 = *((char*)key2);

  if(k1 > k2)
	return DS_CMP_GRT;
  else if(k1 < k2)
	return DS_CMP_LESS;
  else
	return DS_CMP_EQ;
}

int _copy(bit_node* n, void* key, void* value)
{
  node_key(n) = smalloc(sizeof(char));
  node_value(n) = smalloc(sizeof(int));

  *((char*)node_key(n)) = *((char*)key);
  *((int*)node_value(n)) = *((int*)value);

  return 0;
}

void _print(bit_node* n)
{
  test_printf("[%c](%d)", *((char*)node_key(n)), *((int*)node_value(n)));
}

void _delete(void* key, void* value)
{
  sfree(key);
  sfree(value);
}

int main(void)
{
  int v, i;
  char k;
  rnd_tree* rt;

  init_test_enviroment();
  rt = rnd_create();
  ds_reg_func(rt->tree, _cmp, cmp);
  ds_reg_func(rt->tree, _copy, copy);
  ds_reg_func(rt->tree, _delete, delete);

  test_display_header(":: Create new randomized tree ::");

  for(i = 0; i < MAX; i++) {
	k = random() % 129;
	v = random() % 100;

	if(rt->insert(rt->tree, &k, &v) < 0)
	  test_error("Can't insert item with key %c: %s", k, ds_errmsg());
  }
	  
  dump_tree(bit2base_tree(rt), _print);
  dump_tree_info(bit2base_tree(rt), NULL);
  test_next();
  
  end_test_enviroment();
  exit(EXIT_SUCCESS);
}
